
$(document).ready(function() {
	initTable();
	
	$('body').on('click','#addDiction',addDiction);
});

var table = new Table();
var initTable =function(){
	
    var cloumns = [
		    { 'data': 'id','render': function (data) {return '<input type="checkbox" data-id="' + data + '" name="">';}},
		    {'data': 'dic_name'},
		    {'data': 'dic_code'},
		    {'data': 'create_name'},
		    {'data': 'create_date'},
		    {'data': 'id','render':function(data){return table.delBtn(data)+'<a title="查看字典" href="javascript:;" onclick="toEdit(\'查看字典\',\''+basePath+'/dic/toChildDic?id='+data+'\',\'1\')" class="btn btn-warning radius size-MINI" style="margin-right:10px;" >查看</a>';}}
		  ];
	//设置URL和加载的字段
    table.setting(basePath+"/dic/getDicList",basePath+"/dic/delDic",cloumns);
	//调用表格分页
    table.tablePage();
}


//添加字典
var addDiction = function(){
	var dicName = $('#dicName').val();
	var dicCode = $('#dicCode').val();
	
	if(dicName == "" || dicCode == "" ){
		layer.confirm('请填写相关信息!');
		return false;
	}else{
		var params={
				"dicName":dicName,
		        "dicCode":dicCode,
		};
		
		$.ajax({
			type:"post",
			url:basePath+"/dic/validateCode",
			data:params,
			async:true,
			success:function(data){
				
				if(data.code!="0"){
					layer.msg(data.msg,{time:1000});
					return false;
				}else{

					$.ajax({
						type:"post",
						url:basePath+"/dic/addDic",
						data:params,
						async:true,
						success:function(data){
							if(data.code=="0"){
								 layer.msg('添加成功', {
									    time: 2000, //2s后自动关闭
									    btn: [ '确定']
									  });
								 $("#dicName").val("");
								 $("#dicCode").val("");
								 table.tablePage();
							 }else{
								 layer.msg(data.msg, {
									    time: 2000, //2s后自动关闭
									    btn: [ '确定']
									  });
							 }
						}
					});
				}
			}
		});
		
	}
	
}